<?php

class Respondent extends Model {
	
	function Respondent()
	{
		parent::Model();
	}
	
	/*
	 * Checks if a link exists in the database
	 *
	 * Parameter:
	 * $link	the link that is subject to validation
	 *
	 * Return Value:
	 * returns true if the link exists, false otherwise
	 */
	function isValidLink($link)
	{
		$this->db->from('invitation');
		$this->db->where('link', $link);
		$query = $this->db->get();
		
		if(sizeof($query->row_array())>0)
		{
			return true;
		}
		return false;
	}
	
	/*
	 * Checks if a respondent already responded to his invite
	 *
	 * Parameter:
	 * $link	the link that will map to the respondent
	 *
	 * Return Value:
	 * returns false if the respondent has not answered yet, true otherwise
	 */
	function hasResponded($link)
	{
		$this->db->from('invitation');
		$this->db->where('link', $link);
		$this->db->where('status', 'pending');
		$query = $this->db->get();
		
		if(sizeof($query->row_array())>0)
		{
			return false;
		}
		return true;
	}	
	
	/*
	 * Gets the body of the invitation assigned to a link
	 *
	 * Parameter:
	 * $link	the link that will provide the invitation
	 *
	 * Return Value:
	 * returns the body of the invitation
	 */	
	function getInviteBody($link)
	{
		$this->db->select('body');
		$this->db->from('invitation');
		$this->db->where('link', $link);
		$query = $this->db->get();
		$query = $query->row_array();
		if($query==FALSE)
		{
			return "";
		}
		return $query['body'];
	}
	
	/*
	 * Updates the response from an invite
	 * Inserts a respondent in the respondent table if respondent confirms to answer the survey	
	 *
	 * Parameters:
	 $ $response	the respondent's response (N - rejected, YY - yes and redirect, YN - yes)
	 * $link		the link that maps to the invitation
	 *
	 * Return Value:
	 * none
	 */
	function update($response, $link)
	{
		$data = array('status'=>'approved');
		
		if($response=="N")
		{
			$data['status'] = 'rejected';
		}
		else
		{
			$respondent_id = $this->getRespondentNumber($link);
			$data['respondent_number'] = $respondent_id;
			if($respondent_id < 0)
			{ 
				$this->db->insert('respondent', array("status"=>"pending"));
			
				$this->db->from('respondent');
				$this->db->select('respondent_number');
				$this->db->order_by('respondent_number', 'desc');
				$respondent_id = $this->db->get()->row_array();
				$data['respondent_number'] = $respondent_id['respondent_number'];
			}
		}
		$this->db->where('link',$link);
		$this->db->update('invitation', $data);
	}
	
	/*
	 * Gets the survey id of a link
	 *
	 * Parameter:
	 * $link	the link that maps to the invitation
	 *
	 * Return Value:
	 * returns the survey id of the given link
	 */
	function getSurveyId($link)
	{
		$this->db->select('survey_id');
		$this->db->from('invitation');
		$this->db->where('link', $link);
		$query = $this->db->get();
		$query = $query->row_array();
		if($query==FALSE)
		{
			return "";
		}
		return $query['survey_id'];
	}
	
	/*
	 * Gets the questions pointed by the link
	 *
	 * Parameter:
	 * $link	the link that maps to the invitation
	 *
	 * Return Value:
	 * returns the survey details, questions, and choices 
	 */	
	function fetchQuestions($link)
	{
		$this->db->select('survey_id');
		$this->db->from('invitation');
		$this->db->where('link', $link);
		$query = $this->db->get();
		$query = $query->row_array();
		$survey_id = $query['survey_id'];
		
		$this->db->select('title');
		$this->db->from('survey');
		$this->db->where('survey_id', $survey_id);
		$query = $this->db->get();
		$query = $query->row_array();
		$title = $query['title'];
		
		$this->db->from('question');
		$this->db->where('survey_id', $survey_id);
		$this->db->order_by('question_number', 'asc');
		$query = $this->db->get();
		$question = $query->result_array();
		
		$this->db->from('choices');
		$this->db->where('survey_number', $survey_id);
		$this->db->order_by('choice_number', 'asc');	
		$query = $this->db->get();
		$choices = $query->result_array();
		
		foreach($question as $currQuestion)
		{
			$question[$currQuestion['question_number'] - 1]['choices'] = array();
			foreach($choices as $currChoice)
			{
				if($currQuestion['question_number'] == $currChoice['question_number'])
				{
					$question[$currQuestion['question_number'] - 1]['choices'][$currChoice['choice_number']] = $currChoice['body'];
				}
			}
		}
		
		$data = array('title'=>$title, 'questions' => $question);
		return $data;
	}

	/*
	 * Updates a row in the respondent table
	 *
	 * Parameter:
	 * $contents	the row values
	 * $link		the link that maps to a respondent
	 *
	 * Return Value:
	 * none
	 */
	function updateRespondent($contents, $link)
	{
		$this->db->select('respondent_number');
		$this->db->where('link', $link);
		$this->db->from('invitation');
		$respondent = $this->db->get();
		$respondent = $respondent->row_array();
		$respondent = $respondent['respondent_number'];
		
		$this->db->where('respondent_number', $respondent);
		$this->db->update('respondent', $contents);
	}
	
	/*
	 * Gets the total number of choices in a question
	 *
	 * Parameter:
	 * $survey_number		the survey id which contains the question
	 * $question_number		the question of interest
	 *
	 * Return Value:
	 * returns the number of choices
	 */
	function getNumChoices($survey_number, $question_number)
	{
		$this->db->select('choice_number');
		$this->db->where('survey_number', $survey_number);
		$this->db->where('question_number', $question_number);
		$this->db->from('choices');
		$this->db->order_by('choice_number', 'desc');
		$choices = $this->db->get()->row_array();
		if($choices==FALSE)
		{
			return "";
		}
		return $choices['choice_number'];	
	}
	
	/*
	 * Gets the respondent id from a link
	 *
	 * Parameter:
	 * $link	the link that maps to a respondent
	 *
	 * Return Value:
	 * returns the respondent id
	 */
	function getRespondentNumber($link)
	{
		$this->db->select('respondent_number');
		$this->db->from('invitation');
		$this->db->where('link', $link);
		$query = $this->db->get()->row_array();
		if($query==FALSE)
		{
			return "";
		}
		return $query['respondent_number'];
	}
	
	/*
	 * Inserts answer in the answer table
	 *
	 * Parameter:
	 * $contents	the answer
	 *
	 * Return Value:
	 * none
	 */
	function insertAnswer($contents)
	{
		$this->db->insert('answer', $contents);
	}
	
	
	/*
	 * Gets the begin and end date of a survey
	 *
	 * Parameter:
	 * $survey_id	the survey id representing a survey
	 *
	 * Return Value:
	 * an array containing the begin date and end date of a survey 
	 */
	function getSurveyDate($survey_id)
	{
		$this->db->from('survey');
		$this->db->where('survey_id', $survey_id);
		$query = $this->db->get()->row_array();
								
		if(sizeof($query)>0)
		{
			return array('begin_date' => $query['begin_date'], 'end_date' => $query['end_date']);
		}	
		
	}
	
	/*
	 * Gets the required information from the respondents
	 *
	 * Parameter:
	 * $survey_id	the survey id representing the survey
	 *
	 * Return Value:
	 * returns an array containing the required fields as represented in te required_fields table
	 */
	function getRequiredFields($survey_id)
	{
		$this->db->from('required_fields');
		$this->db->where('survey_id', $survey_id);
		$query = $this->db->get()->row_array();
		if($query==FALSE)
		{
			return "";
		}
		
		return $query;
	}
	
	/*
	 * Gets the particular choice of a question
	 *
	 * Parameters:
	 * $survey_number		the survey id containing the question	
	 * $question_number		the question id containing the choice
	 * $choice_number		the choice id pertaining to the choice
	 *
	 * Return Value:
	 * returns the body of the choice as a result of the given constraints
	 */
	function getChoice($survey_number, $question_number, $choice_number)
	{
		$this->db->from('choices');
		$this->db->select('body');
		$this->db->where('survey_number', $survey_number);	
		$this->db->where('question_number', $question_number);
		$this->db->where('choice_number', $choice_number);
		$query = $this->db->get()->row_array();
		if($query==FALSE)
		{
			return "";
		}
		
		return $query['body'];
	}
	
	/*
	 * Gets the list of regions in the philippines table
	 *
	 * Parameter:
	 * none
	 *
	 * Return Value:
	 * returns an array of the regions, where the key maps to itself
	 */
	function getRegions()
	{
		$this->db->from('philippines');
		$this->db->select('region');
		$this->db->group_by('region');
		$regions =  $this->db->get()->result_array();
		$return = array();
		foreach($regions as $current)
		{
			$return[$current['region']] = $current['region'];	
		}
		return $return;
	}
	
	/*
	 * Gets the list of provinces in the philippines table
	 *
	 * Parameter:
	 * none
	 *
	 * Return Value:
	 * returns an array of the provinces, where the key maps to itself
	 */
	function getProvinces()
	{
		$regions =  $this->getRegions();
		$provinces = array();
		foreach($regions as $region)
		{
			$this->db->from('philippines');
			$this->db->select('province');
			$this->db->where('region', $region);
			$query = $this->db->get()->result_array();
			foreach($query as $item)
			{
				$provinces[$region][$item['province']] = $item['province'];
			}
		}
		
		return $provinces;
	}
	
	/*
	 * Gets the list of cities in the philippines table
	 *
	 * Parameter:
	 * none
	 *
	 * Return Value:
	 * returns an array of the cities, where the key maps to itself
	 */
	function getCities()
	{
		$this->db->from('philippines');
		$this->db->select('province');
		$this->db->group_by('province');
		$provinces =  $this->db->get()->result_array();
		$cities = array();
		foreach($provinces as $province)
		{
			$this->db->from('philippines');
			$this->db->select('city');
			$this->db->where('province', $province['province']);
			$query = $this->db->get()->result_array();
			foreach($query as $item)
			{
				$cities[$province['province']][$item['city']] = $item['city'];
			}
		} 
		return $cities;
	}
	
	/*
	 * Checks where a particular value exists in a table
	 *
	 * Parameters:
	 * $table	the table of interest	
	 * $where	the value of interest
	 * 
	 * Return Value:
	 * returns true if the value exists, false otherwise
	 */
	function exists($table, $where)
	{
		$this->db->from($table);
		foreach($where as $current)
		{
			$this->db->where($current['column'],$current['value']);
		}		
		if(sizeof($this->db->get()->row_array())>0)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	/*
	 * Inserts a row in the invitation table
	 *
	 * Parameters:
	 * $survey_id	the survey id pertaining to the survey	
	 * $body		the body of the invitation
	 * $link		the generated link for the invitation
	 * $email		the email of the respondent
	 *
	 * Return Value:
	 * none
	 */
	function insertInvite($survey_id, $body, $link, $email)
	{
		$this->db->from('invitation');
		$count = $this->db->get()->result_array();
		$count = sizeof($count);
		$count = -1 -$count;
	
		$contents = array('survey_id' => $survey_id,'respondent_number' => $count,'email' => $email, 'body' => $body, 'link' => $link);
		$this->db->insert('invitation', $contents);		
	}
	
	/*
	 * Checks whether an invite has been approved
	 *
	 * Parameter:
	 * $link	the link pertaining to the invite of interest	
	 * 
	 * Return Value:
	 * returns true if the invite is approved, false otherwise
	 */
	function approvedRSVP($link)
	{
		$this->db->from('invitation');
		$this->db->where('link', $link);
		$this->db->where('status', 'approved');
		$query = $this->db->get();
		
		if(sizeof($query->row_array())>0)
		{
			return TRUE;
		}
		return FALSE;
	}	
	
	/*
	 * Gets a list of invitations pending for approval/rejection
	 *
	 * Parameter:
	 * none 
	 *
	 * Return Value:
	 * returns an array of invitations with pending status
	 */
	function getUnanswered()
	{
		$this->db->select('invitation.email, invitation.survey_id, invitation.link');
		$this->db->from('respondent, invitation');
		$this->db->where('respondent.respondent_number = invitation.respondent_number');
		$this->db->where('respondent.status', 'pending');
		return $this->db->get()->result_array();
	}
	
	/*
	 * Checks where a respondent has answered the survey
	 *
	 * Parameter:
	 * $link	the link of the invite sent to the respondent
	 * 
	 * Return Value:
	 * returns true if the respondent has answered, false otherwise
	 */
	function hasAnswered($link)
	{
		$this->db->select('respondent.status');
		$this->db->from('respondent, invitation');
		$this->db->where('respondent.respondent_number = invitation.respondent_number');
		$this->db->where('invitation.link', $link);
		$query =  $this->db->get()->row_array();
		
		if($query['status']=="done")
		{
			return TRUE;
		}
		return FALSE;
	}
	
	/*
	 * Gets the notification schedule of a survey
	 *
	 * Parameter:
	 * $survey_id	the survey id of the survey of interest
	 * 
	 * Return Value:
	 * returns the notification sched
	 */
	function getNotifSched($survey_id)
	{
		$this->db->from('survey');
		$this->db->where('survey_id', $survey_id);
		$query = $this->db->get()->row_array();
		
		return $query['notif_sched'];
	}
	
	/*
	 * Gets data of the survey creator
	 *
	 * Parameter:
	 * $survey_id	the survey id of the survey of interest
	 * 
	 * Return Value:
	 * returns an array of the corresponding row of the user table
	 */
	function getUserData($survey_id)
	{
		$this->db->from('user ,survey');
		$this->db->where('survey.user_id = user.user_id');
		$this->db->where('survey.survey_id', $survey_id);
		
		return $this->db->get()->row_array();
	}
	
	/*
	 * Checks where a particular value exists in a table
	 *
	 * Parameter:
	 * $table	the table of interest	
	 * $where	the value of interest
	 * 
	 * Return Value:
	 * returns true if the value exists, false otherwise
	 */
	function getCategories($survey_id)
	{
		$this->db->select('category_name');
		$this->db->from('categories');
		$this->db->where('survey_id', $survey_id);
		
		return $this->db->get()->result_array();
	}
	
	/*
	 * Gets the questions of a survey
	 *
	 * Parameter:
	 * $survey_id	the survey id of the survey of interest	
	 * 
	 * Return Value:
	 * returns the survey details, questions, and choices
	 */
	function fetchQuestionsViaId($survey_id)
	{
		$this->db->select('title');
		$this->db->from('survey');
		$this->db->where('survey_id', $survey_id);
		$query = $this->db->get();
		$query = $query->row_array();
		$title = $query['title'];
		
		$this->db->from('question');
		$this->db->where('survey_id', $survey_id);
		$this->db->order_by('question_number', 'asc');
		$query = $this->db->get();
		$question = $query->result_array();
		
		$this->db->from('choices');
		$this->db->where('survey_number', $survey_id);
		$this->db->order_by('choice_number', 'asc');	
		$query = $this->db->get();
		$choices = $query->result_array();
		
		foreach($question as $currQuestion)
		{
			$question[$currQuestion['question_number'] - 1]['choices'] = array();
			foreach($choices as $currChoice)
			{
				if($currQuestion['question_number'] == $currChoice['question_number'])
				{
					$question[$currQuestion['question_number'] - 1]['choices'][$currChoice['choice_number']] = $currChoice['body'];
				}
			}
		}
		
		$data = array('title'=>$title, 'questions' => $question);
		return $data;
	}
	
	/*
	 * Gets the description of a survey
	 *
	 * Parameter:
	 * $survey_id	the survey id of the survey of interest	
	 * 
	 * Return Value:
	 * returns the description of the survey
	 */
	function getSurveyDescription($survey_id)
	{
		$this->db->from('survey');
		$this->db->where('survey_id', $survey_id);
		$query = $this->db->get()->row_array();
		return $query['description'];
	}
	
	/*
	 * Gets the notification body of a survey
	 *
	 * Parameter:
	 * $survey_id	the survey id of the survey of interest	
	 * 
	 * Return Value:
	 * returns the notification body
	 */
	function getNotificationBody($survey_id)
	{
		$this->db->from('survey');
		$this->db->where('survey_id', $survey_id);
		$query = $this->db->get()->row_array();
		return $query['notif_body'];
	}
}

?>
